package com.net.tool;

import android.content.Context;
import android.content.Intent;

public class DownloadManager {

	Context mContext;

	static DownloadManager mDownloadManager;

	public static DownloadManager getManager(Context context) {

		if (mDownloadManager == null) {
			mDownloadManager = new DownloadManager();

		}

		mDownloadManager.mContext = context;

		return mDownloadManager;

	}

	/**
	 * 追加课程下载任务
	 * 
	 * @param taskId
	 */
	public void appentSessionTask(String taskId, String title, String taskIcon) {

		synchronized (BasicDownload.class) {

			BasicDownload download = DownLoadRunable.getAllRunningList().get(
					taskId);

			if (download == null) {
				new DownloadPlugTooles(mContext, taskId, taskIcon, title);
			}

		}

		updateTask();

	}

	/**
	 * 追加升级下载任务
	 */
	public void appentUpdateTask(String taskId) {

	}

	public void appentAppTask(String taskId) {

	}

	/**
	 * 
	 */

	/**
	 * 暂停下载任务
	 * 
	 * @param taskId
	 */

	public void pauseTask(String taskId) {

		synchronized (BasicDownload.class) {

			BasicDownload download = DownLoadRunable.getAllRunningList().get(
					taskId);

			if (download != null) {

				download.cancal();

			} else {

				BasicDownload.updataTaskState(
						BasicDownload.DOWNLOAD_STATTE_CANCAL, taskId, mContext);

			}
		}
		updateTask();

	}

	/**
	 * 开始下载任务
	 * 
	 * @param taskId
	 */
	public void startTask(String taskId) {

		synchronized (BasicDownload.class) {

			BasicDownload download = DownLoadRunable.getAllRunningList().get(
					taskId);
			if (download == null) {
				BasicDownload.updataTaskState(
						BasicDownload.DOWNLOAD_STATE_PREPARE, taskId, mContext);
			}
		}
		updateTask();

	}

	/**
	 * 删除现在任务
	 * 
	 * @param taskId
	 */

	public void removeTask(String taskId) {

		synchronized (BasicDownload.class) {

			BasicDownload download = DownLoadRunable.getAllRunningList().get(
					taskId);
			
			if (download != null) {
				download.remove();
			} else {
				BasicDownload.updataTaskState(
						BasicDownload.DOWNLOAD_STATE_REMOVE, taskId, mContext);
			}
			
		}

		updateTask();
	}

	public void updateTask() {
		mContext.startService(new Intent(mContext, YogaResDownLoadService.class));

	}

}
